<?
/**
 * 本文件用于查询地点信息
 *
 * 使用方式
 * <code>
 *    $list = UCLibLocation::getLocationList(1); // 获取下级地点列表
 *         // array(2 => "海淀区",3 => "东城区", 4 => "西城区")
 *    $list = UCLibLocation::getLocationPath(1020); // 返回整个地点列表（包含名称）
 *         // array(1 => "浙江省",1015 => "杭州市", 1020 => "拱墅区")
 *    $list = UCLibLocation::getLocationIdPath(1020); // 返回整个地点列表（不包含名称）
 *         // array(1, 1015, 1020)
 *    $name = UCLibLocation::getName(1020); // 返回地点名称
 *         //拱墅区
 *    $name = UCLibLocation::getFullName(1020); // 返回地点完整的名称
 *         //浙江省杭州市拱墅区
 * </code>
 *
 * @author    徐鹏程 <xupengcheng@snda.com>
 * @version   0.0.1		2010/03/29
 */

define("LOCATION_VERSION", 2);
define("LOCATION_PREFIX", "lc");

class UCLibLocation {
	static $_locations_relations = null;
	static $_locations = null;
	static $_apc_mapping_loaded = false;
	static $_apc_data_loaded = false;


	static function getLocationRelation($id) {
		if (self::$_locations_relations)  {
			return isset(self::$_locations_relations[$id]) ?  self::$_locations_relations[$id] : null;
		}

		if (function_exists("apc_store")) {
			if (self::$_apc_mapping_loaded) {
				return apc_fetch(LOCATION_PREFIX.".lr.".$id);
			}

			if (apc_fetch(LOCATION_PREFIX.".lr.ver") !== LOCATION_VERSION) {
				include_once(dirname(__FILE__).'/UCLibLocationMapping.php');
				self::$_locations_relations = UCLibLocationMapping::$locations_mapping;
				apc_store(LOCATION_PREFIX.".lr.ver", LOCATION_VERSION);
				foreach (self::$_locations_relations as $k => $v) {
					apc_store(LOCATION_PREFIX.".lr.".$k, $v);
				}
			}
			self::$_apc_mapping_loaded = true;
			return apc_fetch(LOCATION_PREFIX.".lr.".$id);
		} else {
			include_once(dirname(__FILE__).'/UCLibLocationMapping.php');
			self::$_locations_relations = UCLibLocationMapping::$locations_mapping;
			return isset(self::$_locations_relations[$id]) ?  self::$_locations_relations[$id] : null;
		}
	}

	static function getLocationData($id) {
		if (self::$_locations)  {
			return isset(self::$_locations[$id]) ?  self::$_locations[$id] : null;
		}

		if (function_exists("apc_store")) {
			if (self::$_apc_data_loaded) {
				return apc_fetch(LOCATION_PREFIX.".ld.".$id);
			}

			if (apc_fetch(LOCATION_PREFIX.".ld.ver") !== LOCATION_VERSION) {
				include_once(dirname(__FILE__).'/UCLibLocationData.php');
				self::$_locations = UCLibLocationData::$locations;
				apc_store(LOCATION_PREFIX.".ld.ver", LOCATION_VERSION);
				foreach (self::$_locations as $k => $v) {
					apc_store(LOCATION_PREFIX.".ld.".$k, $v);
				}
			}
			self::$_apc_data_loaded = true;
			return apc_fetch(LOCATION_PREFIX.".ld.".$id);
		} else {
			include_once(dirname(__FILE__).'/UCLibLocationData.php');
			self::$_locations = UCLibLocationData::$locations;
			return isset(self::$_locations[$id]) ?  self::$_locations[$id] : null;
		}
	}

	/**
	 * 根据地点id获取下级地点列表
	 * @param parent_id: 默认为0，表示获取最上级地点
	 * @return 地点列表，比如
	 *    array(1 => "北京市",20 => "天津市")
	 */
	public static function getLocationList($parent_id = 0) {
		$p = self::getLocationRelation($parent_id);
		if ($p) {
			$result = array();
			foreach ($p as $id) {
				$data = self::getLocationData($id);
				if ($data) $result[$id] = $data[0];
			}
			return $result;
		} else {
			return array();
		}
	}

	/**
	 * 根据地点id获取整个地点列表
	 * @param location_id: 当前地点id
	 * @return 地点列表，比如
	 *    array(1 => "浙江省",1015 => "杭州市", 1020 => "拱墅区")
	 */
	public static function getLocationPath($location_id) {
		$ret = self::getLocationIdPath($location_id);
		$result = array();
		foreach ($ret as $key) {
			$data = self::getLocationData($key);
			if ($data) $result[$key] = $data[0];
		}
		return $result;
	}

	/**
	 * 根据地点id获取整个地点id列表
	 * @param location_id: 当前地点id
	 * @return 地点列表，比如
	 *    array(1, 1015, 1020)
	 */
	public static function getLocationIdPath($location_id) {
		$result = array();
		while ($location_id) {
			
			$data = self::getLocationData($location_id);
			if ($data) {
				$result[] = $location_id;
				$location_id = $data[1];
			} else {
				$location_id = false;
			}
		}
		return array_reverse($result);
	}

	/**
	 * 根据地点id获取地点名称
	 * @param location_id: 当前地点id
	 * @return 地点名称
	 */
	public static function getName($location_id) {
		$data = self::getLocationData($location_id);
		return $data ? $data[0] : "";
	}

	/**
	 * 根据地点id获取完整的地点名称
	 * @param location_id: 当前地点id
	 * @return 完整的地点名称，比如浙江省杭州市拱墅区
	 */
	public static function getFullName($location_id) {
		$result = array();
		while ($location_id) {
			$data = self::getLocationData($location_id);
			if ($data) {
				$result []= $data[0];
				$location_id = $data[1];
			} else {
				$location_id = false;
			}
		}
		return join(" ", array_reverse($result));
	}
}